import http from '@/utils/http'
import { RootThunkAction } from '@/types/store'
import { LoginResponse } from '@/types/data'
import { setToken, removeToken } from '@/utils/token'

type LoginForm = {
  mobile: string
  code: string
}
// type LoginResponse = {
//   message: string
//   data: Token
// }
//获取token
export const login = (data: LoginForm): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.post<LoginResponse>('/authorizations', data)
    const token = res.data.data
    setToken(token)
    dispatch({ type: 'login/token', payload: token })
  }
}
// 获取验证码
export const getCode = (mobile: string) => {
  return async () => {
    await http.get(`/sms/codes/${mobile}`)
  }
}
//退出功能
export const logout = (): RootThunkAction => {
  return (dispatch) => {
    removeToken()
    dispatch({ type: 'login/logout' })
  }
}
